package com.nlabs.slickgwt.client;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class IconButton extends FocusPanel {

	private Image m_image = new Image();
	private MouseHandler mh = new MouseHandler(); 
	private int m_padding = 3;
	
	public IconButton() {
	  super();
	}
	
	public IconButton(String url){
		
		super(); 
		m_image.setUrl(url);
		
		 
		
		
	}
	
	public void setPadding(int p){
		m_padding = p; 
		
	}
	
	public int getPadding() {
		return m_padding; 
	}
	
	public void setImageUrl(String url){
		
		m_image.setUrl(url);
		
	}
	
	public void setImageUrlAndRect(String url, int left, int top, int width, int height){
		
		m_image.setUrlAndVisibleRect(url, left, top, width, height);
		
	}
	
	private void layout() {
		 setSize("100%", "100%");
		 HorizontalPanel hpane = new HorizontalPanel();
		 hpane.setSize("100%", "100%");
		 hpane.setSpacing(m_padding);
		 hpane.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		 hpane.add(m_image);
		 hpane.setCellHorizontalAlignment(m_image, HasHorizontalAlignment.ALIGN_CENTER);
		 setWidget(hpane);
		 setStylePrimaryName("slick-button"); 
		 addMouseOverHandler(mh);
		 addMouseOutHandler(mh) ;
		 addMouseDownHandler(mh);
		 addMouseUpHandler(mh);
		 
	 }
	
	private class MouseHandler implements MouseOverHandler, MouseOutHandler, MouseDownHandler, MouseUpHandler {
	
    	
    	public void onMouseOver(MouseOverEvent event) {
    		addStyleDependentName("hover");
		
    	}

    	public void onMouseOut(MouseOutEvent event) {
		 removeStyleDependentName("hover");
		
    	}

    	public void onMouseDown(MouseDownEvent event) {
		
    		event.preventDefault(); 
    		removeStyleDependentName("hover");
    		addStyleDependentName("active");
    		DOM.setCapture(getElement());
    	}

    	public void onMouseUp(MouseUpEvent event) {
		
    		removeStyleDependentName("active");
    		addStyleDependentName("hover");
    		DOM.releaseCapture(getElement());
    	}
    }
	
	public void onAttach() {
		
		 
		layout();
		super.onAttach();
	}
	
}
